//#include <iostream>
//#include <queue>
//using namespace std;
//
//int main() {
//    int n;
//    cin >> n;
//    priority_queue<int, vector<int>, greater<int>> heap;
//    for (int i = 0; i < n; i++) {
//        int weight = 0;
//        cin >> weight;
//        heap.push(weight);
//    }
//    int res = 0;
//    while (heap.size() > 1) {
//        int a = heap.top();
//        heap.pop();
//        int b = heap.top();
//        heap.pop();
//        res += a + b;
//        heap.push(a + b);
//    }
//    cout << res << endl;
//}


#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

struct TN {
    int value;
    TN* left;
    TN* right;
    TN(int v) : value(v), left(nullptr), right(nullptr) {}
};

TN* buildT(const vector<int>& post, const vector<int>& in, int& postIdx, int inStart, int inEnd) {
    if (inStart > inEnd) return nullptr;
    TN* root = new TN(post[postIdx--]);
    int inIdx = find(in.begin() + inStart, in.begin() + inEnd + 1, root->value) - in.begin();
    root->right = buildT(post, in, postIdx, inIdx + 1, inEnd);
    root->left = buildT(post, in, postIdx, inStart, inIdx - 1);
    return root;
}

int getD(TN* root) {
    if (!root) return 0;
    return max(getD(root->left), getD(root->right)) + 1;
}

void preOT(TN* root, vector<int>& result) {
    if (!root) return;
    result.push_back(root->value);
    preOT(root->left, result);
    preOT(root->right, result);
}

int main() {
    string str;
    while (getline(cin, str)) {
        int n = stoi(str);
        vector<int> post(n), in(n);
        getline(cin, str);
        istringstream postStream(str);
        for (int i = 0; i < n; ++i) {
            postStream >> post[i];
        }
        getline(cin, str);
        istringstream inStream(str);
        for (int i = 0; i < n; ++i) {
            inStream >> in[i];
        }
        int postIdx = n - 1;
        TN* root = buildT(post, in, postIdx, 0, n - 1);
        int depth = getD(root);
        vector<int> preResult;
        preOT(root, preResult);
        cout << depth;
        for (int val : preResult) {
            cout << " " << val;
        }
        cout << endl;
        delete root;
    }
    return 0;
}